#include <stdio.h>
#include <netdb.h> /* getresolvname */
#include <time.h>

/* return the number of seconds it needs to resolve one name. */
/* -1 if an error happens or >20 if it was not possible to resolve the name (no connectivity) */
int __resolv_name()
{
	/* name it will try to resolv */
	char name[]="www.google.com";
	struct hostent * p_host;
	time_t t1, t2;
	
	time(&t1);
	if (t1==-1){
		fprintf(stderr,"error getting time\n");
	}
	p_host = gethostbyname(name); /* it tries to resolve each 5 seconds over 20 seconds */
	time(&t2);
	if (t2==-1){
		fprintf(stderr,"error getting time\n");
	}
        if (p_host == NULL) { /* an error */
		/* herror("error resolving name: "); */
		return 21;
	}
	return t2 - t1;
}

/* return a value between 0 and 100 */
/* 0 : No connectivity. 100 : Perfect connectivity */
int check_connectivity()
{

	int result;
	result = __resolv_name();
	if (result < 0 || result > 20) return 0;
	if (result == 20) return 20;
	result = 20 - result;
	return result * 5;
}

int main(int argc, char **argv)
{
	printf("quality of the connection: %d / 100\n", check_connectivity());
	return 0;
}
